package com.gbb.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;

import com.gbb.common.AppUtils;

/**
 * 根据资源名称或资源ID
 *
 * @author gaobingbing
 * @see {@link Resources#getIdentifier(String,String,String);}
 */
@Deprecated
public class ResourceUtil {

    public ResourceUtil() {
    }

    public static int getId(String fieldName) {
        return getFieldValue("id",fieldName);
    }

    public static int getLayout(String fieldName) {
        return getFieldValue("layout",fieldName);
    }

    public static int getDrawable(String fieldName) {
        return getFieldValue("drawable",fieldName);
    }

    public static int getColor(String fieldName) {
        return getFieldValue("color",fieldName);
    }

    public static int getString(String fieldName) {
        return getFieldValue("string",fieldName);
    }

    /**
     * 根据给定的类型名和字段名，返回R文件中的字段的值
     *
     * @param typeName 属于哪个类别的属性 （id,layout,drawable,string,color,attr......）
     * @param fieldName 字段名
     * @return 字段的值
     */
    public static int getFieldValue(String typeName,String fieldName) {
        int i = -1;
        try {
            Class<?> clazz = Class.forName(AppUtils.get().getPackageName() + ".R$" + typeName);
            i = clazz.getField(fieldName).getInt(null);
        } catch(Exception e) {
            Log.e("查找资源","没有找到" + AppUtils.get().getPackageName() + ".R$" + typeName + "类型资源 " + fieldName + "请copy相应文件到对应的目录.");
        }
        return i;
    }

    /**
     * 获取styleable资源
     *
     * @param fieldName 资源名
     * @return
     * @throws ClassNotFoundException
     */
    public static int[] getStyleable(String fieldName) throws ClassNotFoundException {
        try {
            Class<?> clazz = Class.forName(AppUtils.get().getPackageName() + ".R$styleable");
            return (int[])clazz.getField(fieldName).get(null);
        } catch(Exception e) {
            throw new ClassNotFoundException("没有找到自定义属性 " + fieldName + "请copy相应文件到对应的目录.");
        }
    }

    /**
     * 根据“文件名”读取“assets”中的文件内容
     *
     * @param mContext 上下文
     * @param fileName 文件名
     */
    public static String geFileFromAssets(Context mContext,String fileName) {
        if(mContext == null || TextUtils.isEmpty(fileName)) {
            return null;
        }

        StringBuilder s = new StringBuilder("");
        try {
            InputStreamReader in = new InputStreamReader(mContext.getResources().getAssets().open(fileName));
            BufferedReader br = new BufferedReader(in);
            String line;
            while((line = br.readLine()) != null) {
                s.append(line);
            }
            return s.toString();
        } catch(IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据“raw”资源id读取“raw”下的资源 <br/>
     * get content from a raw resource. This can only be used with resources
     * whose value is the name of an asset files -- that is, it can be used to
     * open drawable, sound, and raw resources; it will fail on string and color
     * resources.
     *
     * @param mContext 上下文
     * @param resId 资源ID
     */
    public static String geFileFromRaw(Context mContext,int resId) {
        if(mContext == null) {
            return null;
        }

        StringBuilder s = new StringBuilder();
        try {
            InputStreamReader in = new InputStreamReader(mContext.getResources().openRawResource(resId));
            BufferedReader br = new BufferedReader(in);
            String line;
            while((line = br.readLine()) != null) {
                s.append(line);
            }
            return s.toString();
        } catch(IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * same to {@link ResourceUtil#geFileFromAssets(Context,String)}, but
     * return type is List<String>
     *
     * @param context
     * @param fileName
     * @return
     */
    public static List<String> geFileToListFromAssets(Context context,String fileName) {
        if(context == null || TextUtils.isEmpty(fileName)) {
            return null;
        }

        List<String> fileContent = new ArrayList<>();
        try {
            InputStreamReader in = new InputStreamReader(context.getResources().getAssets().open(fileName));
            BufferedReader br = new BufferedReader(in);
            String line;
            while((line = br.readLine()) != null) {
                fileContent.add(line);
            }
            br.close();
            return fileContent;
        } catch(IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * same to {@link ResourceUtil#geFileFromRaw(Context,int)}, but return type
     * is List<String>
     *
     * @param context
     * @param resId
     * @return
     */
    public static List<String> geFileToListFromRaw(Context context,int resId) {
        if(context == null) {
            return null;
        }

        List<String> fileContent = new ArrayList<>();
        BufferedReader reader = null;
        try {
            InputStreamReader in = new InputStreamReader(context.getResources().openRawResource(resId));
            reader = new BufferedReader(in);
            String line = null;
            while((line = reader.readLine()) != null) {
                fileContent.add(line);
            }
            reader.close();
            return fileContent;
        } catch(IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}
